//
//  LLMusicViewController.swift
//  新印象
//
//  Created by wyl on 2016/12/21.
//  Copyright © 2016年 魏延龙. All rights reserved.
//

import UIKit

class LLMusicViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate {

    var tableView : UITableView!
    var viewModel : LLMusicVM!
    var btn : UIButton!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "音乐"
        self.automaticallyAdjustsScrollViewInsets = false
        self.viewModel = LLMusicVM()
        loadUI()

    }
    
    func loadUI() {
        
        let textfield = UITextField()
        textfield.delegate = self
        textfield.borderStyle = .roundedRect
        textfield.placeholder = "请输入歌名"
        self.view.addSubview(textfield)
        textfield.addTarget(self, action: #selector(textFieldChange(textField:)), for: .editingChanged)
        btn = UIButton(type: .system)
        btn.isEnabled = false
        btn.setTitle("搜索", for: .normal)
        btn.setTitleColor(.blue, for: .normal)
        self.view.addSubview(btn)
        
        textfield.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(66)
            make.left.equalTo(self.view).offset(10)
            make.height.equalTo(44)
            make.right.equalTo(btn.snp.left).offset(-20)
        }
        btn.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(66)
            make.right.equalTo(self.view).offset(-10)
            make.height.equalTo(44)
            make.width.equalTo(80)
        }
        btn.addTarget(self, action: #selector(loadData(sender:)), for: .touchUpInside)
        
        
        tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "music")
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(130)
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-44)
        }
    }
    
    func loadData(sender : UIButton){
        self.viewModel.query(completeSuccess: { 
            self.tableView.reloadData()
        }, completeFailed: {
            print("llmusicvc 失败")
        })
    }
    
    // MARK: UITableViewDelegate,UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.numOfRow()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "music", for: indexPath)
        cell.textLabel?.text = self.viewModel.alldata[indexPath.row].filename
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let playMusicViewController = LLPlayMusicViewController()
        playMusicViewController.viewModel.MusicModelArr = self.viewModel.alldata
        playMusicViewController.viewModel.index = indexPath.row
        self.present(playMusicViewController, animated: true) {
            
        }
    }
    
    // MARK:  UITextFieldDelegate
    func textFieldChange(textField: UITextField) {
        if textField.text == nil {
            btn.isEnabled = false
        } else {
            btn.isEnabled = true
        }
        self.viewModel.filename = textField.text
        print(textField.text!)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }


}
